﻿// SKNotes - a note taking and gathering tool
// Copyright (C) 2009  Stefan Kueng
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

namespace SKNotes
{
    using System;
    using SKNotes.CustomControls;
    using SKNotes.Utilities;

    [Serializable]
    public class NoteTag : BindableObject
    {
        private readonly InvokeOC<NoteTag> tags = new InvokeOC<NoteTag>(App.Current.Dispatcher);
        private string tagName = null;
        private string tagPath = null;
        private string searchString = null;
        [NonSerialized]
        private NoteBook nBook = null;

        public NoteTag(string tagPath, NoteBook noteBook)
        {
            this.TagPath = tagPath;
            int colonIndex = tagPath.LastIndexOf(':');
            this.TagName = colonIndex < 0 ? tagPath : tagPath.Substring(colonIndex + 1);
            this.NoteBook = noteBook;
        }

        public InvokeOC<NoteTag> Tags
        {
            get { return this.tags; }
        }

        public string TagName
        {
            get
            {
                return this.tagName;
            }

            set
            {
                if (this.tagName != null)
                {
                    string parentPath = string.Empty;
                    int index = this.tagPath.LastIndexOf(':');
                    if (index >= 0)
                    {
                        parentPath = this.tagPath.Substring(0, index);
                    }

                    if (parentPath.Length == 0)
                    {
                        parentPath = value;
                    }
                    else
                    {
                        parentPath += ":" + value;
                    }

                    if (!this.tagPath.Equals(parentPath, StringComparison.Ordinal))
                    {
                        this.tagPath = parentPath;
                        this.RaisePropertyChanged("TagPath");
                    }
                }
                else
                {
                    this.tagName = value;
                }

                if (!this.tagName.Equals(value, StringComparison.Ordinal))
                {
                    this.tagName = value;
                    this.RaisePropertyChanged("TagName");
                }
            }
        }

        public string TagPath
        {
            get
            {
                return this.tagPath;
            }

            set
            {
                if (this.tagPath == null)
                {
                    this.tagPath = value;
                }
                else if (!this.tagPath.Equals(value, StringComparison.Ordinal))
                {
                    this.tagPath = value;
                    this.RaisePropertyChanged("TagPath");
                    int index = this.tagPath.LastIndexOf(':');
                    string newName = this.tagPath;
                    if (index >= 0)
                    {
                        newName = this.tagPath.Substring(index + 1);
                    }

                    if (!this.tagName.Equals(newName, StringComparison.Ordinal))
                    {
                        this.tagName = newName;
                        this.RaisePropertyChanged("TagName");
                    }
                }
            }
        }

        public NoteBook NoteBook
        {
            get
            {
                return nBook;
            }

            set
            {
                nBook = value;
            }
        }

        public string SearchString
        {
            get
            {
                return this.searchString;
            }
            set
            {
                if (string.IsNullOrEmpty(this.searchString) || (!this.searchString.Equals(value)))
                {
                    this.searchString = value;
                    this.RaisePropertyChanged("SearchString");
                    this.RaisePropertyChanged("IsSearch");
                }
            }
        }

        public bool IsSearch
        {
            get { return !string.IsNullOrEmpty(this.searchString); }
        }
    }
}
